<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>欢迎回来</h2>
        <p>登录您的博客账户</p>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-position="top" class="login-form">
        <el-form-item label="用户名/邮箱" prop="username">
          <el-input v-model="loginForm.username" :prefix-icon="User" placeholder="请输入用户名或邮箱"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" :prefix-icon="Lock" placeholder="请输入密码"
                    show-password></el-input>
        </el-form-item>
        <div class="remember-forgot">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-link type="primary" @click="forgotPassword">忘记密码？</el-link>
        </div>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="submitForm">登录</el-button>
        </el-form-item>
        <div class="register-link">
          还没有账号？
          <el-link type="primary" @click="goToRegister">立即注册</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {reactive, ref} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {User, Lock} from '@element-plus/icons-vue'
import {login} from '@/api/user'

export default {
  name: "LoginView",
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)

    const loginForm = reactive({
      username: '',
      password: '',
    })

    const rules = {
      username: [
        {required: true, message: '请输入用户名或邮箱', trigger: 'blur'}
      ],
      password: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 6, message: '密码长度不能小于6位', trigger: 'blur'}
      ]
    }

    const submitForm = () => {
      loginFormRef.value.validate((valid) => {
        if (valid) {
          login(loginForm).then(res => {
            if (res.code === 200) {
              const user = res.data.user
              window.localStorage.setItem('user', JSON.stringify(user))

              const token = res.data.token
              window.localStorage.setItem('token', token)

              ElMessage.success('登录成功！')
              router.push('/')

            } else {
              ElMessage.error(res.msg)
            }
          })


        } else {
          ElMessage.error('请正确填写表单！')
          return false
        }
      })
    }

    const goToRegister = () => {
      router.push('/register')
    }

    const forgotPassword = () => {
      ElMessage.info('忘记密码功能即将上线')
      // 可以跳转到忘记密码页面
      // router.push('/forgot-password')
    }

    return {
      loginForm,
      loginFormRef,
      rules,
      submitForm,
      goToRegister,
      forgotPassword,
      User,
      Lock
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 460px;
  padding: 40px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 16px;
  color: #909399;
}

.login-form {
  margin-top: 20px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 4px;
}

.register-link {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}
</style>
